package com.jxtti.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxtti.entity.ArticleList;
import com.jxtti.entity.SiteLog;
import com.jxtti.entity.TDocDocumentsrelease;
import com.jxtti.entity.TSysFilesInfo;
import com.jxtti.service.ArticleListService;
import com.jxtti.service.SiteLogService;
import com.jxtti.service.TDocDocumentsreleaseService;
import com.jxtti.service.TSysFilesInfoService;
import com.jxtti.utils.CommConstant;
import com.jxtti.utils.DateUtil;
import com.jxtti.utils.FileOperate;

public class DocDataTimerTask extends TimerTask {
	private final static Logger log= Logger.getLogger(DocDataTimerTask.class);
	// Servrice start
	@Autowired
	private TDocDocumentsreleaseService  tDocDocumentsreleaseService; 
	
	@Autowired //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private ArticleListService  articleListService; 
	
	@Autowired
	private TSysFilesInfoService  tSysFilesInfoService; 
	
	@Autowired
	private SiteLogService  siteLogService; 
	
	
	@Override
	public void run() {
		log.debug(DateUtil.getCurrDateTime() +"开始同步！！！");
		DocDocumentsreleaseMain();
		log.debug(DateUtil.getCurrDateTime() +"同步结束!!!!");
	}

	
	
	/**
	 * 数据同步入口.获取要同频的数据.
	 *     1. 获取同步数据.并放入到网站中.
	 *     2. 更新同步数据状态. fbzt=1
	 *     3. 同步公文附件   通过公文内容获取公文附件.
	 *     
	 * @return
	 */
	public void DocDocumentsreleaseMain()
	{
		SiteLog siteLog = new SiteLog();
		String tbBeginTime = DateUtil.getCurrDateTime() +"开始同步！！！";
		Map<String , Object> data = new HashMap<String, Object>();
		data.put("jszt", "0");
		data.put("gsdw", "20111229101704767016000000000000");
		List<TDocDocumentsrelease> lst  = tDocDocumentsreleaseService.queryDocStelease(data);
		log.info("同步数据记录数:" + lst.size());
		for(TDocDocumentsrelease  tdd: lst)
		{
			ArticleList tem = saveArticleList(tdd);  //保存公文。
			String filePath = saveDoc_SysFileInfo(tdd); //下载附件, 并更新article_list文 表.
			tem.setFilesPath(filePath);  
			this.articleListService.update(tem);
			updateTDocDocumentsrelease(tdd);         //更新OA公文状态.
		}
		siteLog.setLogtime(DateUtil.getCurrentDate());
		siteLog.setBroswer("后台公文同步!");
		String endTbEndTime = DateUtil.getCurrDateTime() +"同步结束!!!!";
		siteLog.setOp_function_desc(tbBeginTime.concat(" 同步记录数为：" + lst.size() ).concat(";  ").concat(endTbEndTime));
		siteLogService.add(siteLog);
		
	}
	
	
	public String saveDoc_SysFileInfo(TDocDocumentsrelease doc)
	{
		Map<String , Object> data = new HashMap();
		data.put("object_id", doc.getId());
		List<TSysFilesInfo> lst = this.tSysFilesInfoService.queryAllByDoc(data);
		StringBuffer fullPath =new StringBuffer();  //文件名： 访问路径,文件名： 访问路径
		for(TSysFilesInfo info : lst)
		{
			try {
				String temFileName = FileOperate.downLoad_OA_Doc_Attach(info.getFilepath(), info.getFile_name());
				String temURLFilePath = CommConstant.DOC_URL_PATH + temFileName;
				fullPath.append( info.getFile_name() ).append(":").append(temURLFilePath).append(",");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return fullPath.toString();
		
	}
	
	
	
	
	//网站保存公文.
	public ArticleList saveArticleList(TDocDocumentsrelease tdoc)
	{
		ArticleList article = new ArticleList();  
		if(!(tdoc.getFilepath() =="" || tdoc.getFilepath().equals("")))
		{
			String filename="";
			try {
				filename = FileOperate.downLoad_OA_Doc(tdoc.getFilepath());
				
				String content ="<a textvalue='"+filename+"' title='"+filename+"' target='_blank' href='"+CommConstant.DOC_URL_PATH + filename+"'>"+filename+"</a>";
				article.setContents(content);
			} catch (IOException e) {
			
				e.printStackTrace();
			}
			
		}
		article.setTitle(tdoc.getBt());
		article.setCreateDate(tdoc.getFbrq());
		article.setModifyDate(new Date());
		article.setTypeID(9);  //标识来自OA
		article.setCategoryID("4"); //文章类目.
		article.setFk_orgId("36070002");
		article.setArticleSite(0);
		article.setCategoryName("环保公文");
		article.setOrgIdName("局办公室");
		article.setIssue(1); // 起用.
		article.setStatus(2); //装态启用.
		articleListService.add(article);
		return article;
	}
	
	
	/**
	 * 更新同步后的数的公文数据。.
	 * @param data
	 * @return
	 */
	public int updateTDocDocumentsrelease(TDocDocumentsrelease tdoc)
	{
		tdoc = this.tDocDocumentsreleaseService.queryTDocDocumentsreleaseById(tdoc);
		tdoc.setJszt("1");
		return this.tDocDocumentsreleaseService.update(tdoc);
	}
	
	
	
}
